module.exports = (sequelize, DataTypes) => {
  const CodeExample = sequelize.define('CodeExample', {
    id: {
      type: DataTypes.INTEGER,
      primaryKey: true,
      autoIncrement: true,
      comment: '主键ID'
    },
    lessonId: {
      type: DataTypes.INTEGER,
      allowNull: false,
      field: 'lesson_id',
      comment: '课程ID'
    },
    title: {
      type: DataTypes.STRING(200),
      allowNull: false,
      comment: '示例标题'
    },
    code: {
      type: DataTypes.TEXT('long'),
      allowNull: false,
      comment: '代码内容'
    },
    language: {
      type: DataTypes.STRING(50),
      allowNull: false,
      comment: '编程语言'
    },
    description: {
      type: DataTypes.TEXT,
      comment: '代码说明'
    },
    fileName: {
      type: DataTypes.STRING(255),
      field: 'file_name',
      comment: '文件名'
    },
    sortOrder: {
      type: DataTypes.INTEGER,
      defaultValue: 0,
      field: 'sort_order',
      comment: '排序权重'
    },
    isRunnable: {
      type: DataTypes.BOOLEAN,
      defaultValue: false,
      field: 'is_runnable',
      comment: '是否可运行'
    }
  }, {
    tableName: 'code_examples',
    underscored: true,
    timestamps: true,
    createdAt: 'created_at',
    updatedAt: 'updated_at',
    comment: '代码示例表',
    indexes: [
      {
        fields: ['lesson_id']
      },
      {
        fields: ['language']
      },
      {
        fields: ['sort_order']
      }
    ]
  });

  CodeExample.associate = (models) => {
    CodeExample.belongsTo(models.Lesson, {
      foreignKey: 'lessonId',
      as: 'lesson',
      onDelete: 'CASCADE'
    });
  };

  return CodeExample;
}; 